﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SosoEngine.Interface.Map;
using SosoEngine.Const;
using SosoEngine.Simple.Impl.Utilities;

namespace SosoEngine.Simple.Impl.Map
{
    public class SimpleMap : IMap
    {
        #region IMap Members

        MapNoteValue[,] _Nodes;

        /// <summary>
        /// Gets or sets the nodes.
        /// </summary>
        /// <value>The nodes.</value>
        public MapNoteValue[,] Nodes
        {
            get
            {
                return _Nodes;
            }
            set
            {
                _Nodes = value;
            }
        }

        int _Width = -1;
        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>The width.</value>
        public int Width
        {
            get
            {
                return _Width;
            }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentOutOfRangeException("Invalid width of map!");
                }
                _Width = value;
                ResizeMap();
            }
        }

        int _Height = -1;
        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>The height.</value>
        public int Height
        {
            get
            {
                return _Height;
            }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentOutOfRangeException("Invalid height of map!");
                }

                _Height = value;
                ResizeMap();
            }
        }

        /// <summary>
        /// Resizes the map.
        /// </summary>
        private void ResizeMap()
        {
            if (Width < 1 || Height < 1)
            {
                return;
            }

            _Nodes = ArrayHelper.Redim(_Nodes, typeof(MapNoteValue), Width, Height) as MapNoteValue[,];

            if (_Nodes == null)
            {
                throw new ArgumentNullException("Map nodes initilized fail!");
            }
        }

        /// <summary>
        /// Gets the <see cref="SosoEngine.Const.MapNoteValue[]"/> with the specified X.
        /// </summary>
        /// <value></value>
        public MapNoteValue? this[int X, int Y]
        {
            get
            {
                if (_Nodes == null)
                {
                    return null;
                }
                else if (X > Width - 1 || Y > Height -1)
                {
                    throw new ArgumentOutOfRangeException("indicator value out of map nodes axis");
                }
                else
                {
                    return _Nodes[X,Y];
                }
            }
            set
            {
                if (_Nodes == null || X < 0 || X > Width - 1 || Y < 0 || Y > Height - 1 || !value.HasValue)
                {
                    return;
                }
                _Nodes[X, Y] = value.Value;
            }
        }

        #endregion
    }
}
